package priv.conceit.sc.hoxton.auth.core.chain;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import priv.conceit.sc.hoxton.common.entity.auth.authority.pojo.User;
import priv.conceit.sc.hoxton.common.entity.auth.authority.qo.UserLoginQO;
import priv.conceit.sc.hoxton.common.web.exception.WebException;
import priv.conceit.sc.hoxton.common.web.exception.WebExceptionEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * sc-hoxton
 * 执行用户登陆账户验证类型的执行链调用
 *
 * @author Conceit
 * @since 1.0.0, 2020/11/4
 */
@Configuration
public class UserLoginAccountVerifyChain {

	@Autowired
	@Qualifier(value = "usernameHandler")
	UserLoginAccountChainHandler usernamePasswordHandler;

	@Autowired
	@Qualifier(value = "phoneNoHandler")
	UserLoginAccountChainHandler phoneNoPasswordHandler;

	@Autowired
	@Qualifier(value = "jobNumberHandler")
	UserLoginAccountChainHandler jobNumberHandler;

	/**
	 * 过滤器链
	 */
	private List<UserLoginAccountChainHandler> handlers=new ArrayList<>();

	/**
	 * doHandler 执行过滤器链
	 *
	 * @param userLoginQO
	 * @return priv.conceit.sc.hoxton.auth.entity.pojo.AuthUser
	 * @author conceit
	 * @date 2020/11/4 17:22
	 */
	public User doHandler(UserLoginQO userLoginQO){
		for(UserLoginAccountChainHandler handler:handlers){
			User authUser=handler.doHandler(userLoginQO);
			if(Objects.isNull(authUser)){
				continue;
			}
			return authUser;
		}
		//todo 没有过滤器可以执行
		throw new WebException(WebExceptionEnum.EX_USER_ACCOUNT_ERROR);
	}

	/**
	 * init 初始化执行链 加载并装配责任链的顺序
	 *
	 * @return void
	 * @author conceit
	 * @date 2020/11/6 11:19
	 */
	public void init(){
		handlers.add(jobNumberHandler);
		handlers.add(usernamePasswordHandler);
		handlers.add(phoneNoPasswordHandler);
	}


}
